﻿

CREATE proc [dbo].[SaveLog]
@UserID int,
@LogType nchar(10),
@LogArea nchar(10),
@LogDetails nvarchar(max),
@IsError bit,
@LogDateTime datetime2(7) = null

as
begin
	declare @errorMsg nvarchar(150),
			@logDate datetime2(7)

	if @LogType is null
	begin
		set @errorMsg = '@LogType is null or empty.'
		goto WithError
	end

	if @LogArea is null
	begin
		set @errorMsg = '@LogArea is null or empty.'
		goto WithError
	end

	if @LogDetails is null
	begin
		set @errorMsg = '@LogDetails is null or empty.'
		goto WithError
	end

	if @IsError is null
	begin
		set @errorMsg = '@IsError is null or empty.' 
		goto WithError
	end
	
	set @logDate = @LogDateTime

	if @LogDateTime is null
	begin
		set @logDate = sysdatetime()
	end

	begin tran

	insert into dbo.hlpLog (
				UserID,
				LogType,
				LogArea,
				LogDateTime,
				LogDetails,
				IsError
				)
	values	(	@UserID,
				@LogType,
				@LogArea,
				@logDate,
				@LogDetails,
				@IsError
			)
				
	commit

	return 0

WithError:
	if @@TRANCOUNT != 0
	begin
		rollback tran
	end
	RAISERROR
    (@errorMsg,
    10, -- Severity.
    1, -- State.
    @UserID, -- First substitution argument.
    @LogDetails); -- Second substitution argument. 
	return -1 
end
GO
GRANT EXECUTE
    ON OBJECT::[dbo].[SaveLog] TO PUBLIC
    AS [dbo];

